package com.example.sensors;

import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.example.singletones.contextObj;

public class Sensor_Screen extends AbstractSensor {
	public static final String NAME = "SCREEN";
	private Sensor_Screen_Helper mSensor_Screen_Helper = new Sensor_Screen_Helper();

	public Sensor_Screen() {
	}

	private class Sensor_Screen_Helper extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Sensor_Screen.this.onReceive(context, intent);
		}
	}

	public void disableSensor() {
		super.disableSensor();
		contextObj.getInstance().getmContext()
				.unregisterReceiver(mSensor_Screen_Helper);
	}

	public void enableSensor() {
		super.enableSensor();

		contextObj
				.getInstance()
				.getmContext()
				.registerReceiver(mSensor_Screen_Helper,
						new IntentFilter(Intent.ACTION_SCREEN_OFF));
		contextObj
				.getInstance()
				.getmContext()
				.registerReceiver(mSensor_Screen_Helper,
						new IntentFilter(Intent.ACTION_SCREEN_ON));
		contextObj
				.getInstance()
				.getmContext()
				.registerReceiver(mSensor_Screen_Helper,
						new IntentFilter(Intent.ACTION_USER_PRESENT));
	}

	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
			// do whatever you need to do here
			Map<String, Object> eventArgs = initEventArgs();
			eventArgs.put("eventType", "sensorInfoReceived");
			eventArgs.put("screenActivity", "OFF");
			eventArgs.put("userActivity", "NO");
			OnSensorChanged(eventArgs);
		} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
			// and do whatever you need to do here
			Map<String, Object> eventArgs = initEventArgs();
			eventArgs.put("eventType", "sensorInfoReceived");
			eventArgs.put("screenActivity", "ON");
			eventArgs.put("userActivity", "NO");
			OnSensorChanged(eventArgs);
		}
		if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
			Map<String, Object> eventArgs = initEventArgs();
			eventArgs.put("eventType", "sensorInfoReceived");
			eventArgs.put("screenActivity", "ON");
			eventArgs.put("userActivity", "YES");
			OnSensorChanged(eventArgs);
		}

	}

	public String getSensorName() {
		return NAME;
	}
}
